<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RegisterFormValidator
 *
 * @author flocki
 */
namespace Taxx\UserBundle\FormValidators;

use Taxx\KernelBundle\Model\FormValidator\FormValidator;
use Doctrine\ORM\EntityManager;

class RegisterFormValidator 
{
    private $em;
    private $request_params;
    
    public function __construct(EntityManager $em, array $request_params)
    {
        $this->em = $em;
        $this->request_params = $request_params;
    }
    
    public function validate()
    {
        $error = false;
        $err_msg = array();
        
        if(FormValidator::isEmpty($this->request_params['username']))
        {
            $error = true;
            array_push($err_msg, "RegisterForm -> username -> empty");
        }
        
        if(FormValidator::isEmpty($this->request_params['email']))
        {
            $error = true;
            array_push($err_msg, "RgeisterForm -> email -> empty");
        }
        
        if(FormValidator::isEmpty($this->request_params['password']))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> password -> empty");
        }
        
        if(FormValidator::isEmpty($this->request_params['confirm_password']))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> confirm_password -> empty");
        }
           
        if(FormValidator::isSetInDatabase($this->em, 'User', array('username'=>$this->request_params['username'])))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> username -> already_in_use");
        }
        
        if(FormValidator::isSetInDatabase($this->em, 'User', array('email'=>$this->request_params['email'])))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> email -> already_in_use");
        }
        
        if(!FormValidator::isValidEmail($this->request_params['email']))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> email -> not_valid");
        }
        
        if(!FormValidator::isSameValue($this->request_params['password'], $this->request_params['confirm_password']))
        {
            $error = true;
            array_push($err_msg, "RegeisterForm -> password -> no_confirm_password_match");
        }
        
        return array("error"=>$error, "error_msg"=>$err_msg);
    }
}

?>
